<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\OrderHistory;
use App\Models\User;

use DB;

class PayController extends Controller
{   

    //公共支付请求接口 
    public function paypub($api,$price,$mobile){

        //沙箱URL
        $url = 'https://iosdatarecovery.test2.huduntech.com';
        //正式URL
        // $url = 'http://api.huifudashi.com';
        //获取服务器时间
        $time = $url . "/v1/ios/time.php";
        //请求服务器，获取服务器时间
        $ch = curl_init($time);
        $options = array(
                            CURLOPT_RETURNTRANSFER => true,
                        );
        curl_setopt_array($ch,$options);
        $result = curl_exec($ch);
        curl_close($ch);

        //获取服务器时间
        $time = json_decode($result,true)['responseData']['time'];
        //拼接服务器时间并md5加密 在拼接时间,获的sign //短信签名(sign)
        $sign = md5('ZGZhc2ZqYW9zZm5tbGFmajE1NDU=' . $time) . $time;
        //s_id:产品id 周点点第一款产品id为38 苏浩第一款产品id为42 类型：int
        $s_id = 42;
        // softname:产品名称 进行urlencode 类型：string
        $softname = urlencode('先锋恢复精灵');
        // version:产品版本 进行urlencode 类型：string
        $version = urlencode('v1.0');
        // seller_version:收款版本 默认为1 类型：int
        $seller_version = 1;

        $data = $url . $api . '?s_id=' .  $s_id . '&softname=' . $softname . '&price=' . $price . '&version=' . $version . '&mobile=' . $mobile . '&device_id=123' . '&seller_version=' . $seller_version . '&sign=' . $sign;

        //初始化,请求信息并返回数据
        $url = curl_init($data);
        $options = array(
                            CURLOPT_RETURNTRANSFER => true,
                        );
        curl_setopt_array($url,$options);
        $result = curl_exec($url);
        curl_close($url);

        //返回数据
        return $result;
    }

    //升级会员 {mobile} 手机号 {money} 付款金额
    public function vip(Request $request){

        // //请求接口
        $api = '/v1/ios/pay_ali.php';
        //获取返回数据
        $result = $this->paypub($api,$request->money,$request->mobile);

        //获取订单号
        $order = json_decode($result,true)['responseData'];

        //获取当前用户名称
        $user = DB::table('users')->where('phone',$request->mobile)->select('name')->first();
        //数据集合
        $data = $request->only(['name','phone','num','price','priceOK','status','created_at']);
        //用户名
        $data['name'] = $user->name;
        //用户手机
        $data['phone'] = $request->mobile;
        //数量
        $data['num'] = 1;
        //实付款
        $data['priceOK'] = $request->money;
        //单价
        $data['price'] = $request->money;
        //状态
        $data['status'] = 0;
        //时间
        $data['created_at'] = date('Y-m-d H:i:s');
        //会员订单数据入库
        DB::table('order_vip')->insert($data);

        return ['code'=>1,'mobile'=>$request->mobile,'responseData'=>$order];

        
    }

    //接收vip状态通知
    public function vipstatus($mobile){

        //修改订单状态
        DB::table('order_vip')->where('phone',$mobile)->update(['status'=>1]);
        //修改会员状态
        $code = DB::table('users')->where('phone',$mobile)->update(['vip'=>'1']);

        return ['code'=>$code];

    }
    //支付宝原生支付接口支付请求
    // {mobile} 用户手机 ;{sid} 服务id;{num} 服务数量; {money} 付款金额
    public function pay(Request $request){

        //查出未付款订单数量
        $num = OrderHistory::where([
                                    ['mobile','=',$request->mobile],
                                    ['order_status','=','待付款']
                                ])->count();

        //订单限制不可下单
        if($num >= 4)
        {   
            //返回错误
            $code = -1;

            return ['status'=>$code,'success'=>'待付款订单不可以超过4个'];exit;
        }
        //$pice 价格 $mobile 手机号 
        //请求接口
        $api = '/v1/ios/pay_ali.php';

        //获取返回数据
        $result = $this->paypub($api,$request->money,$request->mobile);
    
        //获取返回状态 1 为成功
        $code = json_decode($result,true)['code'];

        // //获取订单号
        $data = json_decode($result,true)['responseData'];

        //读取订单
        $order = substr($data,strrpos($data,"out_trade_no")+21,16);

        //入库数据集合
        $shuju = $request->only(['order_number','mobile','sid','num','money','order_status','serve_status','created_at']);
        
        //订单号
        $shuju['order_number'] = $order;
        //订单状态
        $shuju['order_status'] = '待付款';
        //服务状态
        $shuju['serve_status'] = 0;
        //下单时间
        $shuju['created_at'] = date('Y-m-d H:i:s');

        //数据入库
        OrderHistory::insert($shuju);

        return ['status'=>$code,'order'=>$order,'responseData'=>$data];

    }


    //接收支付通知
    public function paystatus($order){

        //更新数据
        $status = OrderHistory::where('order_number',$order)->update(['order_status'=>'已预约']);

        return ['code'=>$status];

    }


    //支付 待付款原订单
    //{yuan_order} 订单号;{mobile} 用户手机 ; {money} 付款金额
    public function payzf(Request $request){

        //请求接口
        $api = '/v1/ios/pay_ali.php';

        //获取返回数据
        $result = $this->paypub($api,$request->money,$request->mobile);
    
        //获取返回状态 1 为成功
        $code = json_decode($result,true)['code'];

        // //获取订单号
        $data = json_decode($result,true)['responseData'];

        //读取订单
        $order = substr($data,strrpos($data,"out_trade_no")+21,16);

        //入库数据集合
        $shuju = $request->only(['order_number','mobile','sid','num','money','order_status','serve_status','created_at']);
        
        //订单号
        $shuju['order_number'] = $order;
        //订单状态
        $shuju['order_status'] = '待付款';
        //服务状态
        $shuju['serve_status'] = 0;
        //下单时间
        $shuju['created_at'] = date('Y-m-d H:i:s');

        //数据更新
        OrderHistory::where('order_number',$request->yuan_order)->update($shuju); 
        
        return ['code'=>1,'order'=>$order,'responseData'=>$data];

    }
    //待付款订单 mobile 手机号 
    public function stay($mobile){

        //查出待付款订单
        $stay = OrderHistory::where([
                                        ['mobile','=',$mobile],['order_status','=','待付款'],
                                    ])
                                ->LeftJoin('serves','order_histories.sid','=','serves.id')
                                ->select('order_histories.*','serves.title','serves.is_price','serves.price','serves.path','serves.commit')
                                ->OrderBy('order_histories.id','desc')
                                ->get();

        //返回订单状态
        return ['code'=>1,'stay'=>$stay];
    }
    //已预约订单 mobile 手机号 
    public function make($mobile){

        //查出已预约订单
        $stay = OrderHistory::where([
                                        ['mobile','=',$mobile],['order_status','=','已预约']
                                    ])
                                    ->LeftJoin('serves','order_histories.sid','=','serves.id')
                                    ->select('order_histories.*','serves.title','serves.is_price','serves.price','serves.path','serves.commit')
                                    ->OrderBy('id','desc')->get();
        //返回订单状态
        return ['code'=>1,'stay'=>$stay];
    }
    //已取消订单 mobile 手机号 
    public function cancel($mobile){
        
        //查出已取消订单
        $stay = OrderHistory::where([
                                        ['mobile','=',$mobile],['serve_status','=','6']
                                    ])
                                ->LeftJoin('serves','order_histories.sid','=','serves.id')
                                ->select('order_histories.*','serves.title','serves.is_price','serves.price','serves.path','serves.commit')
                                ->OrderBy('id','desc')->get();

        //返回订单状态
        return ['code'=>1,'stay'=>$stay];
    }
    //已评价订单 mobile 手机号 
    public function evaluate($mobile){
        
        //查出已预约订单
        $stay = OrderHistory::where([
                                        ['mobile','=',$mobile],['serve_status','=','2']
                                    ])
                                ->LeftJoin('serves','order_histories.sid','=','serves.id')
                                ->select('order_histories.*','serves.title','serves.is_price','serves.price','serves.path','serves.commit')
                                ->OrderBy('id','desc')->get();

        //返回订单状态
        return ['code'=>1,'stay'=>$stay];
    }

    //申请取消订单
    public function  orderclose($order,$status,$tuikuan){

        //$order 订单号 $status 状态
         $status = OrderHistory::where('order_number',$order)->update(['serve_status'=>$status,'u_commit'=>$tuikuan]);
         
         //执行失败
        $code = 0;
         //判断是否执行成功
         if($status){

            $code = 1;
         }
        return ['code'=>$code];
    }

    //获取评价分数 {order} 订单号 {content} 内容 {score} 分数
    public function score(Request $request){

        //更新订单评价
        $status = OrderHistory::where('order_number',$request->order)->update(['serve_status'=>2,'u_score'=>$request->score,'u_commit'=>$request->content]);

        return ['code'=>$status];
    }

}
